package com.dexilaonian.adpter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.activity.NewsActivity;
import com.dexilaonian.bean.CheckBoxBean;
import com.dexilaonian.bean.NewsBean;
import com.dexilaonian.utils.Logutil;

import java.util.List;

/**
 * Created by Administrator on 2018/3/15.
 */

public class NewsAdapter extends BaseAdapter {

    private final Context mContext;
    private final List<NewsBean> mNewList;
    private boolean isChecked = false;
    private OncheckboxListener listener;

    public NewsAdapter(Context context, List<NewsBean> newList) {
        mContext = context;
        mNewList = newList;
    }

    /**
     * 显示
     */
    public void showCheckbox() {
        isChecked = true;
        notifyDataSetChanged();
    }

    /**
     * 隐藏
     */
    public void hideCheckbox() {
        isChecked = false;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mNewList.size();
    }

    @Override
    public Object getItem(int position) {
        return mNewList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_news, null);
        TextView system_news = (TextView) convertView.findViewById(R.id.news_system);
        TextView main_news = (TextView) convertView.findViewById(R.id.news_main);
        TextView minor_news = (TextView) convertView.findViewById(R.id.news_min);
        TextView time_news = (TextView) convertView.findViewById(R.id.news_time);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox_item);
        NewsBean newsBean = mNewList.get(position);
        minor_news.setText(mNewList.get(position).getMessage());
        time_news.setText(mNewList.get(position).getCreateTime());
        Log.e("TTTTnnnn", mNewList.size() + "=====" + mNewList.get(position).getMessage());
        main_news.setText(mNewList.get(position).getTitle());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                NewsActivity newsActivity = (NewsActivity) mContext;
//                newsActivity.checkedStateChange(position,isChecked);
                if (listener != null) {
                    listener.oncheckboxListener(isChecked, position);
                }
            }
        });
        if (isChecked) {
            checkBox.setVisibility(View.VISIBLE);
        } else {
            checkBox.setVisibility(View.GONE);
        }
        // 最好去掉复用
        return convertView;
    }
    public interface OncheckboxListener {
        void oncheckboxListener(boolean isSelct, int postion);
    }

    public void setCheckboxListener(OncheckboxListener listener) {
        this.listener = listener;
    }

}
